package weaver.interfaces.zlldjcws.cgxxjl.service.impl;

import weaver.interfaces.zlldjcws.util.Message;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.interfaces.zlldjcws.cgxxjl.bean.CgxxBean;
import weaver.interfaces.zlldjcws.cgxxjl.service.LdCgxxService;

import javax.ejb.Stateless;
import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ZhangLei
 * @date 2021/8/3
 * @tableName formtable_main_155
 * @note ZJTBS字段在xml格式中存在，但数据库和excel中没有该字段
 */

@Stateless(mappedName = "LdCgxxServiceImpl")
@WebService(serviceName = "LdCgxxServiceImpl")
public class LdCgxxServiceImpl extends BaseBean implements LdCgxxService {
    @Override
    public Message cgxxData(CgxxBean cgxxBean) {
        Message message = new Message();
        writeLog("表字段对象值是：");
        writeLog(cgxxBean);
//        需要添加到数据库中的字段
        String[] params = {"LIFNR","MATNR","INFNR","ESOKZ","VERKF","TELF1","MEINS","UMREN","UMREZ","URZLA","ZFLAG","LOEKZ","EKORG","APLFZ","EKGRP","NORBM","MINBM","UEBTO","MWSKZ","WEBRE","NETPR","WAERS","PEINH","BPRME","KSCHL","PRDAT","BPUMN","BPUMZ","LOEKZ1","ZYXQBS","DATAB","DATBI","KSTBM","KONMS","KBETR","ZTJYW","ZCZTJ"};
        Map<String,Object> datas = new HashMap<>();
        datas.put("lifnr",cgxxBean.getLifnr());
        datas.put("matnr",cgxxBean.getMatnr());
        datas.put("infnr",cgxxBean.getInfnr());
        datas.put("esokz",cgxxBean.getEsokz());
        datas.put("verkf",cgxxBean.getVerkf());
        datas.put("telf1",cgxxBean.getTelf1());
        datas.put("meins",cgxxBean.getMeins());
        datas.put("umren",cgxxBean.getUmren());
        datas.put("umrez",cgxxBean.getUmrez());
        datas.put("urzla",cgxxBean.getUrzla());
        datas.put("zflag",cgxxBean.getZflag());
        datas.put("loekz",cgxxBean.getLoekz());
        datas.put("ekorg",cgxxBean.getEkorg());
        datas.put("aplfz",cgxxBean.getAplfz());
        datas.put("ekgrp",cgxxBean.getEkgrp());
        datas.put("norbm",cgxxBean.getNorbm());
        datas.put("minbm",cgxxBean.getMinbm());
        datas.put("uebto",cgxxBean.getUebto());
        datas.put("mwskz",cgxxBean.getMwskz());
        datas.put("webre",cgxxBean.getWebre());
        datas.put("netpr",cgxxBean.getNetpr());
        datas.put("waers",cgxxBean.getWaers());
        datas.put("peinh",cgxxBean.getPeinh());
        datas.put("bprme",cgxxBean.getBprme());
        datas.put("kschl",cgxxBean.getKschl());
        datas.put("prdat",cgxxBean.getPrdat());
        datas.put("bpumn",cgxxBean.getBpumn());
        datas.put("bpumz",cgxxBean.getBpumz());
        datas.put("loekz1",cgxxBean.getLoekz1());
        datas.put("zyxqbs",cgxxBean.getZyxqbs());
        datas.put("datab",cgxxBean.getDatab());
        datas.put("datbi",cgxxBean.getDatbi());
        datas.put("kstbm",cgxxBean.getKstbm());
        datas.put("konms",cgxxBean.getKonms());
        datas.put("kbetr",cgxxBean.getKbetr());
        datas.put("ztjyw",cgxxBean.getZtjyw());
        datas.put("zcztj",cgxxBean.getZcztj());

        String sql=insertSql(params,datas,"formtable_main_155");
        writeLog("插入的SQL是：");
        writeLog(sql);
        RecordSet insertRs = new RecordSet();
        boolean insertResult = insertRs.execute(sql);
        if (insertResult){
            message.setFlag("S");
            message.setMessage("Success!");
            message.setId("200");
        }else {
            message.setFlag("E");
            message.setMessage("数据库插入失败，请检查传递的参数");
            message.setId("-200");
        }
        return message;
    }


    /**
     * insert语句的组装
     *
     * @param params    表字段名的String数组
     * @param datas     存储表字段及其对应值的键值对
     * @param tableName 表名
     */
    private static String insertSql(String[] params, Map<String, Object> datas, String tableName) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(tableName).append("(");
        for (int i = 0; i < params.length; i++) {
            if (i != params.length - 1) {
                sql.append(params[i]).append(",");
            } else {
                sql.append(params[i]).append(")");
            }
        }
        sql.append("values(");
        for (int i = 0; i < params.length; i++) {
            if (i != params.length - 1) {
                if (datas.get(params[i].toLowerCase()) == null){
                    sql.append("null");
                }else {
                    sql.append("\'").append(datas.get(params[i].toLowerCase())).append("\'");
                }

                sql.append(",");
            } else {
                if (datas.get(params[i].toLowerCase()) == null){
                    sql.append("null");
                }else {
                    sql.append("\'").append(datas.get(params[i].toLowerCase())).append("\'");
                }
                sql.append(")");
            }
        }
        return sql.toString();
    }

}
